package com.ruitu.mall_98_1.product.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.bumptech.glide.Glide;
import com.ruitu.mall_98_1.R;
import com.ruitu.mall_98_1.product.activity.PhotoCheckActivity;
import com.ruitu.mall_98_1.product.activity.ProductDetailActivity;
import com.ruitu.router_module.bean.product.Evaluate;
import com.ruitu.router_module.utils.GlideUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/10.
 */
public class EvaluateListAdapter extends ListBaseAdapter<Evaluate> {
    public EvaluateListAdapter(Context context) {
        super(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.pro_item_detail_evaluate;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        RecyclerView rcv_img = holder.getView(R.id.rcv_img);
        ImageView iv_header = holder.getView(R.id.iv_header);
        TextView tv_user_name = holder.getView(R.id.tv_user_name);
        TextView tv_time = holder.getView(R.id.tv_time);//发表时间
        TextView tv_content = holder.getView(R.id.tv_content);

        Evaluate eva = mDataList.get(position);

        Glide.with(Arad.app).load(Config.BASE_URL + eva.getHeadImage()).apply(Arad.opts_circle).into(iv_header);
        tv_user_name.setText(eva.getAccountName());
        tv_time.setText(new SimpleDateFormat("yyyy-MM-dd").format(eva.getCreateTime()));
        tv_content.setText(eva.getContent());

        List<Evaluate.EvaluateImg> imgList = eva.getCommentImages();
        if (null != imgList && imgList.size() > 0) {//设置评论图片
            rcv_img.setVisibility(View.VISIBLE);
            LinearLayoutManager manager = new LinearLayoutManager(mContext);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);

            ProImgAdapter adapter = new ProImgAdapter(mContext);
            rcv_img.setLayoutManager(manager);
            rcv_img.setAdapter(adapter);

            adapter.setDataList(imgList);
        } else {
            rcv_img.setVisibility(View.GONE);
        }
    }

    public static class ProImgAdapter extends ListBaseAdapter<Evaluate.EvaluateImg> {

        public ProImgAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.pro_item_evaluate_img;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            ImageView iv_pro = holder.getView(R.id.iv_pro);

            Evaluate.EvaluateImg img = mDataList.get(position);
            GlideUtil.loadImg(Config.BASE_URL + img.getUrl(), iv_pro);

            iv_pro.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //点击查看商品图片
                    Intent intent = new Intent(mContext, PhotoCheckActivity.class);

                    intent.putExtra("position", position);
                    ArrayList<String> imgPaths = new ArrayList<>();
                    for (Evaluate.EvaluateImg i : mDataList) {//封装图片路径,传到图片查看页面
                        imgPaths.add(Config.BASE_URL + i.getUrl());
                    }
                    intent.putExtra("list", imgPaths);

                    mContext.startActivity(intent);
                    ((ProductDetailActivity) mContext).overridePendingTransition(
                            R.anim.fade_in, R.anim.fade_out);
                }
            });
        }
    }
}
